﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Globalization;

namespace School.Pages.Bus
{
    public partial class BUS_Student_Temp : System.Web.UI.Page
    {

        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1203) == true)
                {
                    if (!this.Page.IsPostBack)
                        FillControls();
                    lbltst.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
                ddlBind(ddlbus, School.Service.Bus.GetBusByCamp(Convert.ToInt32(ddlCampus.SelectedValue.ToString())).ToList());
                ddlBind(ddlbusback, School.Service.Bus.GetBusByCamp(Convert.ToInt32(ddlCampus.SelectedValue.ToString())).ToList());

            }
        }
        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedValue != "0")
            {
                ddlstudent.DataSource = School.Service.Bus.AllFullName_temp(Convert.ToInt32(ddlCampus.SelectedValue), Convert.ToInt32(ddlCertificate.SelectedValue), Convert.ToInt32(ddlStadge.SelectedValue), int.Parse(ddlGrade.SelectedValue));
                ddlBind(ddlstudent);
            }
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlbus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlbus.SelectedValue != "0")
            {
                var q = School.Service.Bus.GetBusDistforddl(int.Parse(ddlbus.SelectedValue));
                ddlDistrict.DataSource = q.ToList();
                ddlBind(ddlDistrict);
            }
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlDistrict.SelectedValue != "0")
            {
                IEnumerable<SRV_Bus_Station> query = School.Service.Bus.GetstationdllBy(int.Parse(ddlDistrict.SelectedValue));
                //  var q = from a in query join b in db.SRV_Bus_Station_Price on a.Id equals b.Station_Id orderby a.Name select a;
                ddlstation.DataSource = query.ToList();
                ddlBind(ddlstation);
            }
        }

        protected void ddlbusback_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlbusback.SelectedValue != "0")
            {
                var q = School.Service.Bus.GetBusDistforddl(int.Parse(ddlbusback.SelectedValue));
                //var qq = from c in q join aa in db.LU_District on c.District_Id equals aa.Id select aa;
                ddlDistrictback.DataSource = q.ToList();
                ddlBind(ddlDistrictback);
            }
        }
        protected void ddlDistrictback_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlDistrictback.SelectedValue != "0")
            {
                IEnumerable<SRV_Bus_Station> query = School.Service.Bus.GetstationdllBy(int.Parse(ddlDistrictback.SelectedValue));
                // var q = from a in query join b in db.SRV_Bus_Station_Price on a.Id equals b.Station_Id orderby a.Name select a;
                ddlstationback.DataSource = query.ToList();
                ddlBind(ddlstationback);
            }
        }


        protected void btnHost_Click(object sender, EventArgs e)
        {
            CultureInfo SaCulture = new CultureInfo("ar-eg");
            DateTimeFormatInfo Form = SaCulture.DateTimeFormat;
            Form.ShortDatePattern = "dd/MM/yyyy";

            int bgoid = 0, bbackid = 0, stgoid = 0, stbackid = 0, Student_Id = 0;
            bgoid = Convert.ToInt32(ddlbus.SelectedValue);
            bbackid = Convert.ToInt32(ddlbusback.SelectedValue);
            stgoid = Convert.ToInt32(ddlstation.SelectedValue);
            stbackid = Convert.ToInt32(ddlstationback.SelectedValue);
            Student_Id = Convert.ToInt32(ddlstudent.SelectedValue);
            bool re = false;

            if (rdblSubscription.SelectedIndex == 0)
            {
                Page.Validate("go");
                if (Page.IsValid)
                {
                    re = School.Service.Bus.Addstudent_to_bus_Temp(Student_Id, bgoid, stgoid, null, null, Convert.ToDateTime(txtfrom.Text, Form), Convert.ToDateTime(txtTo.Text, Form));

                }
            }
            else if (rdblSubscription.SelectedIndex == 1)
            {
                Page.Validate("ba");
                if (Page.IsValid)
                {
                    re = School.Service.Bus.Addstudent_to_bus_Temp(Student_Id, null, null, bbackid, stbackid, Convert.ToDateTime(txtfrom.Text, Form), Convert.ToDateTime(txtTo.Text, Form));

                }
            }
            else
            {
                Page.Validate("temp");
                if (Page.IsValid)
                {
                    re = School.Service.Bus.Addstudent_to_bus_Temp(Student_Id, bgoid, stgoid, bbackid, stbackid, Convert.ToDateTime(txtfrom.Text, Form), Convert.ToDateTime(txtTo.Text, Form));

                }
            }
            if (re == true)
            {

                lbltst.Text = "تم التسكين بنجاح";
            }
            else
            {
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;


        }

        protected void rdblSubscription_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdblSubscription.SelectedIndex == 0)
            {
                PLGo.Visible = true;
                Plback.Visible = false;
                PlBus.Visible = true;
            }
            else if (rdblSubscription.SelectedIndex == 1)
            {

                PLGo.Visible = false;
                Plback.Visible = true;
                PlBus.Visible = true;
            }
            else
            {
                PLGo.Visible = true;
                Plback.Visible = true;
                PlBus.Visible = true;
            }
        }


    }
}